/**
 * 20.有效的括号 使用栈实现
 * @param str 输入的字符串
 * @returns Boolean
 */
function isValid(str: string): boolean {
  if (str.length % 2) {
    return false
  }
  let stack: string[] = []
  for (let i = 0; i < str.length; i++) {
    if (str[i] === '(' || str[i] === '{' || str[i] === '[') {
      stack.push(str[i])
    } else {
      if (stack.length === 0) {
        return false
      }
      const s = stack.pop()
      if (!((str[i] === '}' && s === '{')
        || (str[i] === ']' && s === '[')
        || (str[i] === ')' && s === '('))) {
        return false
      }
    }
  }
  return !stack.length
}

const res01 = isValid('[{()}]')
console.log(res01)
